import type { POIData } from '@/components/HospitalMapViewer'

// 简单的内存级 POI 目录。你可以在任意页面调用 registerPOI(s) 进行注册，
// 然后在目标页面通过 getByIds 解析 URL 中的 poiIds 并直接渲染。
// 若需要持久化，可替换为 IndexedDB/后端服务。

const registry = new Map<string, POIData>()

export function registerPOI(poi: POIData) {
  registry.set(poi.id, poi)
}

export function registerPOIs(pois: POIData[]) {
  pois.forEach(p => registry.set(p.id, p))
}

export function getById(id: string): POIData | undefined {
  return registry.get(id)
}

export function getByIds(ids: string[]): POIData[] {
  return ids
    .map(id => registry.get(id))
    .filter((v): v is POIData => Boolean(v))
}

export function getAllPOIs(): POIData[] {
  return Array.from(registry.values())
}

export function removePOI(id: string): boolean {
  return registry.delete(id)
}

export function clearAll() {
  registry.clear()
}

// 预定义一些示例 POI 供测试使用
export function initDemoPOIs() {
  const demoPOIs: POIData[] = [
    // 主要科室
    {
      id: 'dept_cardio',
      name: '心内科',
      type: 'department',
      position: [120.1555, 30.2745],
      floor: 'L3',
      description: '心血管疾病诊疗科室'
    },
    {
      id: 'dept_emergency',
      name: '急诊科',
      type: 'emergency',
      position: [120.1545, 30.2735],
      floor: 'L1',
      description: '24小时急诊服务'
    },
    {
      id: 'dept_surgery',
      name: '外科',
      type: 'department',
      position: [120.1565, 30.2755],
      floor: 'L2',
      description: '外科手术科室'
    },
    {
      id: 'dept_neurology',
      name: '神经内科',
      type: 'department',
      position: [120.1560, 30.2750],
      floor: 'L4',
      description: '神经系统疾病诊疗'
    },
    {
      id: 'dept_pediatrics',
      name: '儿科',
      type: 'department',
      position: [120.1548, 30.2742],
      floor: 'L2',
      description: '儿童医疗保健科室'
    },
    {
      id: 'dept_orthopedics',
      name: '骨科',
      type: 'department',
      position: [120.1572, 30.2752],
      floor: 'L3',
      description: '骨骼肌肉系统疾病治疗'
    },
    {
      id: 'dept_oncology',
      name: '肿瘤科',
      type: 'department',
      position: [120.1558, 30.2738],
      floor: 'L5',
      description: '恶性肿瘤综合治疗'
    },
    {
      id: 'dept_icu',
      name: '重症监护室',
      type: 'emergency',
      position: [120.1542, 30.2748],
      floor: 'L3',
      description: '危重患者监护治疗'
    },
    
    // 医疗设备
    {
      id: 'equip_mri',
      name: 'MRI设备',
      type: 'equipment',
      position: [120.1550, 30.2740],
      floor: 'L1',
      description: '磁共振成像设备'
    },
    {
      id: 'equip_ct',
      name: 'CT扫描仪',
      type: 'equipment',
      position: [120.1553, 30.2743],
      floor: 'L1',
      description: 'X射线计算机断层扫描'
    },
    {
      id: 'equip_xray',
      name: 'X光机',
      type: 'equipment',
      position: [120.1547, 30.2737],
      floor: 'L1',
      description: 'X射线拍片设备'
    },
    {
      id: 'equip_ultrasound',
      name: '超声检查室',
      type: 'equipment',
      position: [120.1562, 30.2746],
      floor: 'L2',
      description: '超声波诊断设备'
    },
    
    // 服务设施
    {
      id: 'pharmacy_main',
      name: '中心药房',
      type: 'department',
      position: [120.1540, 30.2730],
      floor: 'L1',
      description: '处方药品配发中心'
    },
    {
      id: 'lab_main',
      name: '检验科',
      type: 'department',
      position: [120.1567, 30.2749],
      floor: 'L1',
      description: '医学检验与化验'
    },
    {
      id: 'registration',
      name: '挂号收费处',
      type: 'entrance',
      position: [120.1549, 30.2739],
      floor: 'L1',
      description: '门诊挂号缴费服务'
    },
    {
      id: 'cafeteria',
      name: '医院食堂',
      type: 'entrance',
      position: [120.1570, 30.2760],
      floor: 'L1',
      description: '员工和患者用餐区域'
    },
    
    // 出入口
    {
      id: 'entrance_main',
      name: '医院主入口',
      type: 'entrance',
      position: [120.1551, 30.2741],
      floor: 'L1',
      description: '医院主要出入口'
    },
    {
      id: 'entrance_emergency',
      name: '急诊入口',
      type: 'emergency',
      position: [120.1544, 30.2733],
      floor: 'L1',
      description: '急诊专用出入口'
    },
    {
      id: 'parking_main',
      name: '主停车场',
      type: 'entrance',
      position: [120.1535, 30.2725],
      floor: 'L1',
      description: '医院主要停车区域'
    },
    
    // 垂直交通
    {
      id: 'elevator_a',
      name: 'A区电梯',
      type: 'elevator',
      position: [120.1552, 30.2744],
      floor: 'L1',
      description: 'A区主要电梯'
    },
    {
      id: 'elevator_b',
      name: 'B区电梯',
      type: 'elevator',
      position: [120.1559, 30.2747],
      floor: 'L1',
      description: 'B区主要电梯'
    }
  ]
  
  registerPOIs(demoPOIs)
}

export const poiCatalog = {
  registerPOI,
  registerPOIs,
  getById,
  getByIds,
  getAllPOIs,
  removePOI,
  clearAll,
  initDemoPOIs
}

export default poiCatalog